﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Coolite.Ext.Web;
using System.Configuration;

namespace PeisongWeb.admin
{
    public partial class login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public static string GetClientIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (null == result || result == string.Empty)
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }

            if (null == result || result == string.Empty)
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            return result;
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            // 验证登录代码
            string username = this.txtUsername.Value.Trim();
            string pwd = Library.StringLbl.EncryptMD5(this.txtPassword.Value.Trim());
            SysAdminEntity adminEnt = new SysAdminEntity();
            adminEnt = logic.sysAdmin.login(username, pwd, Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["AfterLoginMin"]));
            if (adminEnt.StatusCode == 3)
            {
                logic.sysAdmin.RecordLogin(adminEnt.AdminId);

                logic.loginLog.insertFade(adminEnt.AdminId, "1", Session.SessionID);

                int psdhistory = logic.sysAdmin.selectPsdhistory(adminEnt.AdminId, "2");
                if (psdhistory == 0)
                {
                    Response.Redirect("firsteditpwd.aspx");
                }
                else
                {
                    bool isAuth = false;
                    string ipAddr = GetClientIP();

                    if (ipAddr == ConfigurationManager.AppSettings["WANIPAddress"] || ipAddr == "127.0.0.1")// LAN Access
                    {
                        isAuth = true;
                    }
                    else
                    {
                        if (ConfigurationManager.AppSettings["EnabledFilterAccess"] == "1")
                        {
                            isAuth = adminEnt.IsAllowAccess;
                        }
                        else
                        {
                            isAuth = true;
                        }
                    }

                    if (isAuth)
                    {
                        Response.Redirect("index.aspx");
                    }
                    else
                    {
                        Page.RegisterStartupScript("", "<script>alert('外部访问权限不够，请联系系统管理员！');</script>");
                        return;
                    }
                }
            }
            else if (adminEnt.StatusCode == 1)
            {
                Page.RegisterStartupScript("", "<script>alert('用户名不存在，请重试！');</script>");
                return;
            }
            else if (adminEnt.StatusCode == 2)
            {
                Page.RegisterStartupScript("", "<script>alert('该用户名已禁用，请重试！');</script>");
                return;
            }
            else if (adminEnt.StatusCode == 4)
            {
                if (adminEnt.ErrorPwdTimes == 3)
                {
                    Page.RegisterStartupScript("", "<script>alert('您已连续三次输入错误的密码，请" + System.Configuration.ConfigurationSettings.AppSettings["AfterLoginMin"] + "分钟后再登陆！');</script>");
                    return;
                }
                else
                {
                    Page.RegisterStartupScript("", "<script>alert('输入密码错误！" + System.Configuration.ConfigurationSettings.AppSettings["AfterLoginMin"] + "分钟内您还有" + (3 - adminEnt.ErrorPwdTimes) + "次机会！');</script>");
                    return;
                }
            }
            else
            {
                Page.RegisterStartupScript("", "<script>alert('您在" + System.Configuration.ConfigurationSettings.AppSettings["AfterLoginMin"] + "分钟内已经连续三次输入错误密码！请在" + (Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["AfterLoginMin"]) - adminEnt.ErrorMin) + "分钟后再登陆！');</script>");
                return;
            }
        }
    }
}
